using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using Alogient.Cameleon.Forms.Sdk;
using Alogient.Cameleon.Forms.Sdk.Items;
using Alogient.Cameleon.Sdk;
using System.Collections;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormSelect : CMFormElement
    {
        public override string Value
        {
            get
            {
                ListControl select = this.FindControl(string.Format("select_{0}", this.ID)) as ListControl;

                if (select.GetType() == typeof(DropDownList))
                    return select.SelectedValue;
                else //ListBox
                {
                    StringBuilder sb = new StringBuilder();
                    foreach(int i in (select as ListBox).GetSelectedIndices())
                        sb.AppendFormat("{0};", i);

                    return sb.ToString();
                }
            }
        }
        public override string[] Values
        {
            get
            {
                ListControl select = this.FindControl(string.Format("select_{0}", this.ID)) as ListControl;

                ArrayList list = new ArrayList();

                if (select.GetType() == typeof(DropDownList))
                    list.Add(select.SelectedValue);
                else //ListBox
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (int i in (select as ListBox).GetSelectedIndices())
                        list.Add(i.ToString());
                }

                return list.ToArray(typeof(string)) as string[];
            }
        }

        internal FormSelect(Forms.Sdk.Items.Select element) : base(element)
        {
            ListControl select;

            if (element.Type == SelectType.Dropdown)
            {
                select = new DropDownList();
            }
            else
            {
                select = new ListBox();

                if (element.MultipleSelect)
                    (select as ListBox).SelectionMode = ListSelectionMode.Multiple;
            }

            //Empty element when not required
            if (!element.Required)
            {
                ListItem item = new ListItem();
                item.Text = string.Empty;
                item.Value = string.Empty;

                select.Items.Add(item);
            }

            select.ID = string.Format("select_{0}", this.ID);
            foreach (FormElementChoice choice in element.Choices)
            {
                if ((CMServices.IsLive && choice.VisibleLive) || (!CMServices.IsLive && choice.VisiblePreview))
                {
                    ListItem item = new ListItem();
                    item.Text = choice.Name;
                    item.Value = choice.ID.ToString();// Value;

                    if (choice.ID == element.DefaultValue)
                        item.Selected = true;

                    select.Items.Add(item);
                }
            }

            select.SelectedValue = element.DefaultValue.ToString();
            select.CssClass = string.Format("{0}Input", element.CSS);


            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            Label redStar = null;
            RequiredFieldValidator required = null;

            if (element.Required)
            {
                redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                required = new RequiredFieldValidator();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = select.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label);
            }

            PlaceHolder phValidators = new PlaceHolder();

            if (element.Required)
            {
                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required);
            }

            #endregion

            GenerateRow(element, label, select, phValidators);
        }
    }
}
